package com.liao.rent.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liao.rent.dto.HouseOrderDto;
import com.liao.rent.dto.reqAddOrder;
import com.liao.rent.entity.HouseOrder;
import com.liao.rent.entity.JsonResult;
import com.liao.rent.service.HouseOrderService;
import com.liao.rent.utils.ResultTool;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author liao
 * @since 2024-12-06
 */
@Tag(name = "房源订单管理")
@RestController
@RequestMapping("/houseOrder")
public class HouseOrderController {

    @Autowired
    HouseOrderService orderService;



    /**
     * 获取订单列表数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Operation(summary = "订单dto列表查询")
    @GetMapping("/list")
    public JsonResult  list(int pageNum, int pageSize,@RequestParam(required = false) Integer ownerUserId,@RequestParam(required = false) Integer customerUserId){
          HouseOrder  order=new HouseOrder();

          if (ownerUserId!=null){
              order.setOwnerUserId(ownerUserId);
          }

          if (customerUserId!=null){
              order.setCustomerUserId(customerUserId);
          }
        Page<HouseOrderDto> list=null;
    list = orderService.list(pageNum, pageSize,order);



        return ResultTool.success(list);
    }




    @Operation(summary = "根据订单id修改订单状态")
    @Transactional
    @PostMapping("/updateStatus/{orderId}/{status}")
    public JsonResult<String> updateStatus(@PathVariable("orderId") Integer orderId, @PathVariable("status")Integer status){

//        构建sql语句
        LambdaUpdateWrapper<HouseOrder> lambdaUpdateWrapper=new LambdaUpdateWrapper();
        lambdaUpdateWrapper.eq(HouseOrder::getOrderId,orderId);
        lambdaUpdateWrapper.set(HouseOrder::getStatus,status);

        orderService.update(lambdaUpdateWrapper);
        return ResultTool.success("修改成功");
    }

    @Operation(summary = "添加订单")
    @PostMapping("/add")
    @Transactional
    public JsonResult<String> add(@RequestBody reqAddOrder requestAddOrder){
        try {

       orderService.add(requestAddOrder);

        }catch (Exception e){
            return ResultTool.fail(e.getMessage());
        }
            return ResultTool.success("添加成功");


    }

}
